package com.csair.youth.dao.impl.hibernate3;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.csair.youth.database.Permission;

/**
 * Impl:auto created by daobuilder1.1.1 
 * @version 1.0
 * @author daobuilder@csair.com
 */
public class PermissionDaoImpl extends com.csair.youth.dao.impl.hibernate3.base.PermissionDaoImplBase{
	private static final String FIND_BY_ROLE_ID_HQL = "from "+Permission.REF+" a where a."+Permission.PROP_ID+" in ";
	private static final String FIND_BY_URL_HQL = "from "+Permission.REF+" a where a."+Permission.PROP_URL +"=:url";
	private static final Logger log = Logger.getLogger(PermissionDaoImpl.class);
	
	public List findPermissionsByIds(String pids[]){
		return this.getGenericDao().find(FIND_BY_ROLE_ID_HQL+buildInClause(pids));
	}
	
	public Permission findFirstPermissionByURL(String url){
		Map map = new HashMap();
		map.put("url", url);
		return this.findFirst(this.getGenericDao().find(FIND_BY_URL_HQL, map, null));
	}
	
	private String buildInClause(String pids[]){
		if(pids == null || pids.length == 0){
			return "";
		}
		StringBuffer sb = new StringBuffer("(");
		for(int i=0; i<pids.length; i++){
			sb.append("'"+pids[i]+"'");
			if(i < pids.length-1){
				sb.append(",");
			}
		}
		sb.append(")");
		String clause = sb.toString();
		log.warn("buildInClause:"+clause);
		return clause;
	}
}